package com.myc.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * druid配置数据库加密
 * */
public class DruidEncrypuUtil {

    //定义公钥和私钥
    private static String publicKey;

    private static String privateKey;

    static {
        try {
            //生成512位的编码
            String[] genKeyPair = ConfigTools.genKeyPair(512);
            privateKey=genKeyPair[0];
            System.out.println("privateKey的值："+privateKey);
            publicKey=genKeyPair[1];
            System.out.println("pbulicKey的值："+publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    //定义加密方法
    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey,plainText);
        System.out.println("加密:" + encrypt);
        return encrypt;
    }
    //定义解密方法
    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey,encryptText);
        System.out.println("解密:" + decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encryptusername = encrypt("root");
        String encryptpwd = encrypt("Zhou11@@");
        String encrypturl = encrypt("124.222.26.15");
        System.out.println("加密后的用户名："+encryptusername);
        System.out.println("加密后的密码："+encryptpwd);
        System.out.println("加密后的地址："+encrypturl);
    }
}
